<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 14/01/2019
 * Time: 17:31
 */

namespace App\Modules\Cargo;

use App\Modules\AdapterApi;
use App\Services\Cargo\CargoIndex;
use App\Services\Fund\FundIndex;
use App\Services\User\UserIndex;

class Cargo extends AdapterApi
{
    /**
     * 发布货物
     * @return mixed
     */
    public function addCargo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        //判断用户是否实名认证
        $status = $userInfo['res']['other_info']->authen_status;
        if($status != 1)
            return auto_failure(null, '您尚通过实名认证');
        return CargoIndex::Cargo()->addCargo($data);
    }

    /**
     * 修改货物信息
     * @return array
     */
    public function changeComCargo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return CargoIndex::Cargo()->changeComCargo($data);
    }

    /**
     * 获取我的货源列表
     * @return mixed
     */
    public function getCargoList()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id']= $userInfo['res']['id'];
        return CargoIndex::Cargo()->getCargoList($data);
    }

    /**
     * 添加常发货源
     */
    public function addCommonCargo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id']= $userInfo['res']['id'];
        return CargoIndex::Cargo()->addCommonCargo($data);
    }

    /**
     * 获取常发货源列表
     * @return array
     */
    public function getComCargoList()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id']= $userInfo['res']['id'];
        return CargoIndex::Cargo()->getComCargoList($data);
    }

    /**
     * 根据id删除常发货源
     * @return array
     */
    public function delComCargo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id']= $userInfo['res']['id'];
        return CargoIndex::Cargo()->delComCargo($data);
    }

    /**
     * 车主找货
     * @return array
     */
    public function findCargos()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo, '请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return CargoIndex::Cargo()->findCargos($data);
    }

    /**
     * 检测用户是否登录成功，
     * 如果登录成功，
     * 则返回用户信息，
     * 否则提示用户登录。
     * @param $token
     * @return array
     */
    private function isLogin()
    {
        $token = ($this->postData)['token'];
        $info = $tokenContent = UserIndex::Token()->hGetAll($token);
        if (!isset($info['res']['id'])) {
            return need_login(null, '请登录');
        }else{
            $info['res']['other_info'] = json_decode($info['res']['other_info']);
            return $info;
        }
    }

    /**
     * 获取货源列表
     * @return mixed
     */
    public function backCargoList()
    {
        $data = $this->postData;
        return CargoIndex::Cargo()->backCargoList($data);
    }

    /**
     * 根据id获取货物详情
     * @return array
     */
    public function getCargoDetail()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return CargoIndex::Cargo()->getCargoDetail($data);
    }

    /**
     * 支付保证金
     * @return array
     */
    public function payDeposit()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return FundIndex::FundAccount()->payDeposit($data);
    }
}
